tokfandomcom-20200215-history
C
C is a , computer supporting , , and , while a prevents unintended operations. By design, C provides constructs that map efficiently to typical , and has found lasting use in applications previously coded in . Such applications include s, as well as various for computers ranging from s to s. C was originally developed at by between 1972 and 1973 to make utilities running on . Later, it was applied to re-implementing the kernel of the Unix operating system. During the 1980s, C gradually gained popularity. Nowadays, it is one of the , with C s from various vendors available for the majority of existing s and operating systems. C has been standardized by the since 1989 (see ) and subsequently by the . C is an language. It was designed to be compiled using a relatively straightforward , to provide access to , to provide language constructs that map efficiently to , and to require minimal . Despite its low-level capabilities, the language was designed to encourage programming. A -compliant C program that is written with in mind can be compiled for a wide variety of computer platforms and operating systems with few changes to its source code; the language has become available on various platforms, from embedded s to s. Overview (right), the inventor of the C programming language, with }} Like most procedural languages in the tradition, C has facilities for and allows and recursion. Its static prevents unintended operations. In C, all is contained within s (also called "functions", though not strictly in the sense of ). are always passed by value. Pass-by-reference is simulated in C by explicitly passing values. C program source text is , using the as a terminator and for grouping . The C language also exhibits the following characteristics: * There is a small, fixed number of keywords, including a full set of primitives: , , , , and . User-defined names are not distinguished from keywords by any kind of . * There are a large number of arithmetic, bitwise and logic operators: +, +=, ++, &, ||, etc. * More than one may be performed in a single statement. * Function return values can be ignored when not needed. * Typing is , but ; all data has a type, but implicit conversions are possible. * mimics usage context. C has no "define" keyword; instead, a statement beginning with the name of a type is taken as a declaration. There is no "function" keyword; instead, a function is indicated by the parentheses of an argument list. * User-defined ( ) and compound types are possible. ** Heterogeneous aggregate data types ( ) allow related data elements to be accessed and assigned as a unit. ** is a structure with overlapping members; only the last member stored is valid. ** indexing is a secondary notation, defined in terms of pointer arithmetic. Unlike structs, arrays are not first-class objects: they cannot be assigned or compared using single built-in operators. There is no "array" keyword in use or definition; instead, square brackets indicate arrays syntactically, for example month11. ** s are possible with the enum keyword. They are freely interconvertible with integers. ** are not a distinct data type, but are conventionally as character arrays. * Low-level access to is possible by converting machine addresses to typed . * (subroutines not returning values) are a special case of function, with an untyped return type void. * Functions may not be defined within the lexical scope of other functions. * Function and data pointers permit ad hoc . * A performs definition, file inclusion, and . * There is a basic form of : files can be compiled separately and together, with control over which functions and data objects are visible to other files via and extern attributes. * Complex functionality such as , manipulation, and mathematical functions are consistently delegated to . While C does not include certain features found in other languages (such as and ), these can be implemented or emulated, often through the use of external libraries (e.g., the or the ). Syntax C has a specified by the C standard. Line endings are generally not significant in C; however, line boundaries do have significance during the preprocessing phase. Comments may appear either between the delimiters /* and */, or (since C99) following // until the end of the line. Comments delimited by /* and */ do not nest, and these sequences of characters are not interpreted as comment delimiters if they appear inside or character literals. C source files contain declarations and function definitions. Function definitions, in turn, contain declarations and . Declarations either define new types using keywords such as struct, union, and enum, or assign types to and perhaps reserve storage for new variables, usually by writing the type followed by the variable name. Keywords such as char and int specify built-in types. Sections of code are enclosed in braces ({ and }, sometimes called "curly brackets") to limit the scope of declarations and to act as a single statement for control structures. As an imperative language, C uses statements to specify actions. The most common statement is an expression statement, consisting of an expression to be evaluated, followed by a semicolon; as a side effect of the evaluation, functions may be and variables may be new values. To modify the normal sequential execution of statements, C provides several control-flow statements identified by reserved keywords. is supported by if(-else) conditional execution and by do-while, while, and for iterative execution (looping). The for statement has separate initialization, testing, and reinitialization expressions, any or all of which can be omitted. break and continue can be used to leave the innermost enclosing loop statement or skip to its reinitialization. There is also a non-structured statement which branches directly to the designated within the function. switch selects a case to be executed based on the value of an integer expression. Expressions can use a variety of built-in operators and may contain function calls. The order in which arguments to functions and operands to most operators are evaluated is unspecified. The evaluations may even be interleaved. However, all side effects (including storage to variables) will occur before the next " "; sequence points include the end of each expression statement, and the entry to and return from each function call. Sequence points also occur during evaluation of expressions containing certain operators (&&, ||, and the ). This permits a high degree of object code optimization by the compiler, but requires C programmers to take more care to obtain reliable results than is needed for other programming languages. Kernighan and Ritchie say in the Introduction of The C Programming Language: "C, like any other language, has its blemishes. Some of the operators have the wrong precedence; some parts of the syntax could be better." The C standard did not attempt to correct many of these blemishes, because of the impact of such changes on already existing software. Operators C supports a rich set of , which are symbols used within an to specify the manipulations to be performed while evaluating that expression. C has operators for: * : , , , , * : = * : +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= * : ~, &, |, ^ * : <<, >> * : !, &&, || * : * equality testing: , * : ( ) * : ++, -- * : ., -> * object size: * : <, <=, >, >= * : &, *, [ ] * sequencing: * : ( ) * : (typename) C uses the operator = (used in mathematics to express equality) to indicate assignment, following the precedent of and , but unlike and its derivatives. C uses the operator to test for equality. The similarity between these two operators (assignment and equality) may result in the accidental use of one in place of the other, and in many cases, the mistake does not produce an error message (although some compilers produce warnings). For example, the conditional expression if(a b+1) might mistakenly be written as if(a=b+1), which will be evaluated as true if a is not zero after the assignment. The C is not always intuitive. For example, the operator binds more tightly than (is executed prior to) the operators & (bitwise AND) and | (bitwise OR) in expressions such as x & 1 0, which must be written as (x & 1) 0 if that is the coder's intent. Data types The in C is and , which makes it similar to the type system of descendants such as . There are built-in types for integers of various sizes, both signed and unsigned, s, and enumerated types (enum). Integer type char is often used for single-byte characters. C99 added a . There are also derived types including , , ( ), and (union). C is often used in low-level systems programming where escapes from the type system may be necessary. The compiler attempts to ensure type correctness of most expressions, but the programmer can override the checks in various ways, either by using a to explicitly convert a value from one type to another, or by using pointers or unions to reinterpret the underlying bits of a data object in some other way. Some find C's declaration syntax unintuitive, particularly for s. (Ritchie's idea was to declare identifiers in contexts resembling their use: " ".) C's usual arithmetic conversions allow for efficient code to be generated, but can sometimes produce unexpected results. For example, a comparison of signed and unsigned integers of equal width requires a conversion of the signed value to unsigned. This can generate unexpected results if the signed value is negative. Pointers C supports the use of , a type of that records the address or location of an object or function in memory. Pointers can be dereferenced to access data stored at the address pointed to, or to invoke a pointed-to function. Pointers can be manipulated using assignment or . The run-time representation of a pointer value is typically a raw memory address (perhaps augmented by an offset-within-word field), but since a pointer's type includes the type of the thing pointed to, expressions including pointers can be type-checked at compile time. Pointer arithmetic is automatically scaled by the size of the pointed-to data type. Pointers are used for many purposes in C. are commonly manipulated using pointers into arrays of characters. is performed using pointers. Many data types, such as , are commonly implemented as dynamically allocated struct objects linked together using pointers. Pointers to functions are useful for passing functions as arguments to s (such as or ) or as to be invoked by event handlers. A value explicitly points to no valid location. Dereferencing a null pointer value is undefined, often resulting in a . Null pointer values are useful for indicating special cases such as no "next" pointer in the final node of a , or as an error indication from functions returning pointers. In appropriate contexts in source code, such as for assigning to a pointer variable, a null pointer constant can be written as 0, with or without explicit casting to a pointer type, or as the NULL macro defined by several standard headers. In conditional contexts, null pointer values evaluate to false, while all other pointer values evaluate to true. Void pointers (void *) point to objects of unspecified type, and can therefore be used as "generic" data pointers. Since the size and type of the pointed-to object is not known, void pointers cannot be dereferenced, nor is pointer arithmetic on them allowed, although they can easily be (and in many contexts implicitly are) converted to and from any other object pointer type. Careless use of pointers is potentially dangerous. Because they are typically unchecked, a pointer variable can be made to point to any arbitrary location, which can cause undesirable effects. Although properly used pointers point to safe places, they can be made to point to unsafe places by using invalid ; the objects they point to may continue to be used after deallocation ( s); they may be used without having been initialized ( s); or they may be directly assigned an unsafe value using a cast, union, or through another corrupt pointer. In general, C is permissive in allowing manipulation of and conversion between pointer types, although compilers typically provide options for various levels of checking. Some other programming languages address these problems by using more restrictive types. Arrays types in C are traditionally of a fixed, static size specified at compile time. (The more recent C99 standard also allows a form of variable-length arrays.) However, it is also possible to allocate a block of memory (of arbitrary size) at run-time, using the standard library's malloc function, and treat it as an array. C's unification of arrays and pointers means that declared arrays and these dynamically allocated simulated arrays are virtually interchangeable. Since arrays are always accessed (in effect) via pointers, array accesses are typically not checked against the underlying array size, although some compilers may provide as an option. Array bounds violations are therefore possible and rather common in carelessly written code, and can lead to various repercussions, including illegal memory accesses, corruption of data, , and run-time exceptions. If bounds checking is desired, it must be done manually. C does not have a special provision for declaring s, but rather relies on within the type system to declare arrays of arrays, which effectively accomplishes the same thing. The index values of the resulting "multi-dimensional array" can be thought of as increasing in . Multi-dimensional arrays are commonly used in numerical algorithms (mainly from applied ) to store matrices. The structure of the C array is well suited to this particular task. However, since arrays are passed merely as pointers, the bounds of the array must be known fixed values or else explicitly passed to any subroutine that requires them, and dynamically sized arrays of arrays cannot be accessed using double indexing. (A workaround for this is to allocate the array with an additional "row vector" of pointers to the columns.) C99 introduced "variable-length arrays" which address some, but not all, of the issues with ordinary C arrays. Memory management One of the most important functions of a programming language is to provide facilities for managing and the objects that are stored in memory. C provides three distinct ways to allocate memory for objects: * : space for the object is provided in the binary at compile-time; these objects have an (or lifetime) as long as the binary which contains them is loaded into memory. * : temporary objects can be stored on the , and this space is automatically freed and reusable after the block in which they are declared is exited. * : blocks of memory of arbitrary size can be requested at run-time using library functions such as from a region of memory called the ; these blocks persist until subsequently freed for reuse by calling the library function or References Category:Minor articles